from flask import Flask, request, jsonify

flk = Flask(__name__)

host = '0.0.0.0'
port = 7777


class CommonReq():
    apiEnvKey = "env"
    apiEnvValue = ""

    startTimeKey = 'startTime'
    endTimeKey = 'endTime'
    startTimeValue = 0
    endTimeValue = 0
    
    commonReq = dict()
    commonReq[apiEnvKey] = apiEnvValue
    commonReq[startTimeKey] = startTimeValue
    commonReq[endTimeKey] = endTimeValue
    
    def __init__(self):
        pass
    
    @classmethod
    def process_request(cls, func=None):
        data = request.get_json()
        view = request.view_args
        if data:
            if all(item in list(data.keys()) for item in list(cls.commonReq.keys())) is False:
                return dict()
            if isinstance(data[cls.startTimeKey], int) is False or (isinstance(data[cls.endTimeKey], int) is False):
                return dict()
            if data[cls.apiEnvKey] is False:
                return dict()
            else:
                if func:
                    return func(view)
                else:
                    return jsonify(data)
        else:
            return dict()
    
    @classmethod
    def checkReq(cls):
        if cls.process_request():
            return True
        else:
            return False



def process_request_decorator(func):
    def wrapper(**kwargs):
        resp = CommonReq.process_request(func)
        if not resp:
            return jsonify({'error': 'Request not valid'})
        return func(**kwargs)
    return wrapper